package com.dzt.cloudreader.webview.config;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dzt.cloudreader.webview.WebViewActivity;


/**
 * Created by jingbin on 2016/11/17.
 * 监听网页链接:
 * - 优酷视频直接跳到自带浏览器
 * - 根据标识:打电话、发短信、发邮件
 * - 进度条的显示
 * - 添加javascript监听
 */
public class MyWebViewClient extends WebViewClient {

	private IWebPageView mIWebPageView;
	private WebViewActivity mActivity;

	public MyWebViewClient(IWebPageView mIWebPageView) {
		this.mIWebPageView = mIWebPageView;
		mActivity = (WebViewActivity) mIWebPageView;

	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		// 优酷视频跳转浏览器播放
		if (url.startsWith("http://v.youku.com/")) {
			Intent intent = new Intent();
			intent.setAction("android.intent.action.VIEW");
			intent.addCategory("android.intent.category.DEFAULT");
			intent.addCategory("android.intent.category.BROWSABLE");
			Uri content_url = Uri.parse(url);
			intent.setData(content_url);
			mActivity.startActivity(intent);
			return true;

			// 电话、短信、邮箱
		} else if (url.startsWith(WebView.SCHEME_TEL) || url.startsWith("sms:") || url.startsWith(WebView.SCHEME_MAILTO)) {
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(url));
				mActivity.startActivity(intent);
			} catch (ActivityNotFoundException ignored) {
			}
			return true;
		}
		mIWebPageView.startProgress();
		view.loadUrl(url);
		return false;
	}

	@Override
	public void onPageFinished(WebView view, String url) {
		if (mActivity.mProgress90) {
			mIWebPageView.hindProgressBar();
		} else {
			mActivity.mPageFinish = true;
		}
		if (!isNetWorkConnected(mActivity)) {
			mIWebPageView.hindProgressBar();
		}
		// html加载完成之后，添加监听图片的点击js函数
		mIWebPageView.addImageClickListener();
		super.onPageFinished(view, url);
	}

	// 视频全屏播放按返回页面被放大的问题
	@Override
	public void onScaleChanged(WebView view, float oldScale, float newScale) {
		super.onScaleChanged(view, oldScale, newScale);
		if (newScale - oldScale > 7) {
			view.setInitialScale((int) (oldScale / newScale * 100)); //异常放大，缩回去。
		}
	}

	/**
	 * 检测网络是否可用
	 *
	 * @param context
	 * @return
	 */
	private boolean isNetWorkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}
}
